<?php
/**
 * Name: 登录控制器.
 * User: 董坤鸿
 * Date: 2017/11/21
 * Time: 16:59
 */

namespace app\admin\controller;

use app\model\Admins;
use think\Controller;
use think\Request;

class Login extends Controller
{
    /**
     * 初始化
     * login constructor.
     * @param Request|null $request
     */
    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        //执行登录验证
        if (session('admin.mobile')) {
            $this->redirect('admin/index/index');
        }
    }

    /**
     * 登陆页面
     *
     * @return \think\response\View
     */
    public function index()
    {
        if (request()->isPost()) {
            $res = (new Admins())->login(input('post.'));
            if ($res['valid']) {
                //说明登录成功
                $this->success($res['msg'], 'admin/index/index');
                exit;
            } else {
                //说明登录失败
                $this->error($res['msg']);
                exit;
            }
        }
        return view();
    }
}